package leetcode.code0020;


/**
 * 100% 击败
 * 数组替代栈
 * @author CaoYu
 * @date 2021年9月26日
 * @description
 */
public class Solution3 extends Solution0020 {

	@Override
	public boolean isValid(String s) {
		if (s.length() < 2) {
			return false;
		}
		char[] chars = s.toCharArray();
		char[] stack = new char[chars.length];
		int pstack = 0;
		for (char element : chars) {
			if (element == '(' || element == '[' || element == '{') {
				stack[pstack++] = element;
			} else {
				if (pstack == 0)
					return false;
				char c = stack[--pstack];
				if ((element == ')' && c != '(') || (element == ']' && c != '[') || (element == '}' && c != '{'))
					return false;
			}
		}
		return pstack==0;
	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
